package com.pinda.controller;


import com.pinda.base.entity.PdAgencyScope;
import com.pinda.service.IPdAgencyScopeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.pinda.base.angency.AgencyScopeDto;
import com.pinda.base.entity.PdAgencyScope;
import com.pinda.base.user.CourierScopeDto;
import com.pinda.commonutils.utils.Result;
import com.pinda.service.IPdAgencyScopeService;
import com.pinda.service.IPdCourierScopeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.pinda.base.entity.PdAgencyScope;
import com.pinda.service.IPdAgencyScopeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pinda.base.angency.AgencyScopeDto;
import com.pinda.base.entity.PdAgencyScope;
import com.pinda.base.user.CourierScopeDto;
import com.pinda.commonutils.utils.Result;
import com.pinda.service.IPdAgencyScopeService;
import com.pinda.service.IPdCourierScopeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import javax.annotation.Resource;

/**
 * <p>
 * 机构业务范围表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-agency-scope")
public class PdAgencyScopeController {
    @Resource
    private IPdAgencyScopeService iPdAgencyScopeService;

    /**
     * 机构范围添加
     * @param pdAgencyScope
     */
    @PostMapping("/jgfanweisave")
    public void jgfanweisave(@RequestBody PdAgencyScope pdAgencyScope) {
        iPdAgencyScopeService.jgfanweisave(pdAgencyScope);

    }
    /**
     * 某一个机构范围查询
     * @param pdAgencyScope
     */
    @PostMapping("/jgfanweiquery")
    public PdAgencyScope jgfanweiquery(@RequestBody PdAgencyScope pdAgencyScope) {

     return   iPdAgencyScopeService.jgfanweiquery(pdAgencyScope);
    }
    @PostMapping("/jgfanweiupdate")
    public void jgfanweiupdate(@RequestBody PdAgencyScope pdAgencyScope) {

          iPdAgencyScopeService.jgfanweiupdate(pdAgencyScope);
    }
    @Resource
    private IPdAgencyScopeService agencyScopService;
    /**
     * 批量保存机构业务范围
     *
     * @param dtoList 机构业务范围信息
     * @return 返回信息
     */
    @PostMapping("/agency/batch")
    public Result batchSaveAgencyScope(@RequestBody List<AgencyScopeDto> dtoList) {
        agencyScopService.batchSave(dtoList.stream().map(dto -> {
            PdAgencyScope scope = new PdAgencyScope();
            BeanUtils.copyProperties(dto, scope);
            if (!ObjectUtils.isEmpty(dto.getMutiPoints())) {
                scope.setMutiPoints(JSON.toJSONString(dto.getMutiPoints()));
            }
            return scope;
        }).collect(Collectors.toList()));
        return Result.ok();
    }

    /**
     * 删除机构业务范围信息
     *
     * @param dto 参数
     * @return 返回信息
     */
    @DeleteMapping("/agency")
    public Result deleteAgencyScope(@RequestBody AgencyScopeDto dto) {
        agencyScopService.delete(dto.getAreaId(), dto.getAgencyId());
        return Result.ok();
    }

    /**
     * 获取机构业务范围列表
     *
     * @param areaId   行政区域id
     * @param agencyId 机构id
     * @return 机构业务范围列表
     */
    @GetMapping("/agency")
    public List<AgencyScopeDto> findAllAgencyScope(@RequestParam(name = "areaId", required = false) String areaId, @RequestParam(name = "agencyId", required = false) String agencyId, @RequestParam(name = "agencyIds", required = false) List<String> agencyIds, @RequestParam(name = "areaIds", required = false) List<String> areaIds) {
        return agencyScopService.findAll(areaId, agencyId, agencyIds, areaIds).stream().map(scope -> {
            AgencyScopeDto dto = new AgencyScopeDto();
            BeanUtils.copyProperties(scope, dto);
            if (StringUtils.isNotBlank(scope.getMutiPoints())) {
                Gson gson = new Gson();
                List<List> json = gson.fromJson(scope.getMutiPoints(),List.class);
                dto.setMutiPoints(json);
            }
            return dto;
        }).collect(Collectors.toList());
    }
}
